const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const City = sequelize.define('City', {
  city_id: { 
    type: DataTypes.INTEGER, 
    primaryKey: true, 
    autoIncrement: true,
    field: 'city_id'  // 明确指定字段名
  },
  name: { 
    type: DataTypes.STRING, 
    allowNull: false,
    field: 'city_name' // 映射到数据库中的 city_name 列
  },
  city_code: {
    type: DataTypes.STRING,
    allowNull: true, // 假设可以为空，根据你的数据库实际情况调整
    field: 'city_code' // 映射到数据库中的 city_code 列
  }
}, {
  tableName: 'Cities',  // 明确指定表名
  timestamps: false     // 禁用时间戳
});

// models/City.js
City.associate = (models) => {
  City.belongsToMany(models.User, {
    through: models.UserCity,
    foreignKey: 'city_id',
    otherKey: 'user_id'
  });
  City.hasMany(models.Weather, { foreignKey: 'city_id', sourceKey: 'city_id' });
};

module.exports = City;